package com.jinken.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jinken.handler.BlockHandler;
import com.jinken.handler.FallBackHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/testA")
    public String testA(){
        return "===============testA===========";
    }

    @RequestMapping("/testB")
    public String testB(){
        return "===============testB===========";
    }

    @GetMapping("/warmUp")
    public String warmUp(){
        return "=========warmUp=========";
    }


    @GetMapping("/testC")
    public String testC(@RequestParam("id") Integer id) throws InterruptedException {

        if(id==1){
            Thread.sleep(1000);
        }

        return "-----------testC----------";
    }

    @GetMapping("/testD")
    public String testD(@RequestParam("id") Integer id) {

        if(id==1){
            System.out.println(1/0);
        }

        return "-----------testD----------";
    }

    //============================降级==============================

    /**
     * @SentinelResource() Sentinel 资源管理配置
     *  fallback : 程序异常，对应的降级资源名称
     *  fallbackClass : 程序异常降级对应的类
     */
    @GetMapping("/testE")
    @SentinelResource(fallback = "fallBack",fallbackClass = FallBackHandler.class)
    public String testE(@RequestParam("id") Integer id) {
        if(id==1){
            System.out.println(1/0);
        }

        return "-----------testE----------";
    }

    /**
     * @SentinelResource() Sentinel 资源管理配置
     *  fallback : 程序异常，对应的降级资源名称
     *  fallbackClass : 程序异常降级对应的类
     *  blockHandler :触发sentinel 流控规则降级方法
     */
    @GetMapping("/testF")
    @SentinelResource(blockHandler = "testblockHandler",blockHandlerClass = BlockHandler.class)
    public String testF(@RequestParam("id") Integer id) {

        return "-----------testF----------"+id;
    }


    //将流控规则降级和java程序异常降级合并一起
    @GetMapping("/testG")
    @SentinelResource(
            fallback = "fallBack",fallbackClass = FallBackHandler.class,
            blockHandler = "testblockHandler",blockHandlerClass = BlockHandler.class
    )
    public String testG(@RequestParam("id") Integer id) {
        if(id==1){
            System.out.println(1/0);
        }

        return "-----------testE----------";
    }

}
